package com.xiaolin.sports.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xiaolin.sports.model.ImgGroup;
import com.xiaolin.sports.model.ParentInfo;
import com.xiaolin.sports.page.Page;
import com.xiaolin.sports.reqVo.CourseSignOrderVO;
import com.xiaolin.sports.service.ICollectionService;
import com.xiaolin.sports.service.IParentInfoService;
import com.xiaolin.sports.view.CourseListView;
import com.xiaolin.sports.view.CourseSignOrderView;
import com.xiaolin.sports.wechat.model.WechatUserInfo;

@RequestMapping("user")
@Controller
public class UserController extends BaseController{
	
	@Autowired
	private IParentInfoService parentInfoService;
	@Autowired
	private ICollectionService collectionService;
	
	private Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@RequestMapping(value = {"", "/"})
	public String my(Model model) {
		WechatUserInfo userInfo = getWechatUserInfo();
		model.addAttribute("userInfo", userInfo);
		ParentInfo parentInfo = parentInfoService.selectByOpenid(userInfo.getOpenid());
		model.addAttribute("parentInfo", parentInfo);
		return "user/my";
	}
	
	/**
	 * 用户绑定
	 * @auther 胡启萌
	 * @Date 2016年11月21日
	 * @return
	 */
	@RequestMapping("bind")
	public String bind(Model model) {
		WechatUserInfo userInfo = getWechatUserInfo();
		int count = parentInfoService.selectCountByOpenid(userInfo.getOpenid());
		model.addAttribute("isBind", count > 0 ? true : false);
		model.addAttribute("openid", userInfo.getOpenid());
		return "user/bind";
	}
	
	/**
	 * 用户购买记录查询
	 * @auther 胡启萌
	 * @Date 2016年11月21日
	 * @param model
	 * @return
	 */
	@RequestMapping("buyRecord")
	public String buyRecord(Model model) {
		WechatUserInfo userInfo = getWechatUserInfo();
		CourseSignOrderVO vo = new CourseSignOrderVO();
		vo.setOpenid(userInfo.getOpenid());
		Page<CourseSignOrderView> page = new Page<>();
		page.setPageNo(1);
		page.setPageSize(Integer.MAX_VALUE - 1);
		page.setParams(vo);
		List<CourseSignOrderView> orderList = productOrderService.selectCourseSignOrderByPage(page);
		ImgGroup group = new ImgGroup();
		group.setPosition("1");
		group.setShelves(1);
		model.addAttribute("topBannerList",contentManageService.selectListByParam(group));
		model.addAttribute("orderList", orderList);
		return "user/buyRecord";
	}
	
	/**
	 * 个人收藏查询
	 * @auther 胡启萌
	 * @Date 2016年11月21日
	 * @param model
	 * @return
	 */
	@RequestMapping("collection")
	public String collection(Model model) {
		WechatUserInfo userInfo = getWechatUserInfo();
		ParentInfo parentInfo = parentInfoService.selectByOpenid(userInfo.getOpenid());
		List<CourseListView> list = collectionService.
				selectListForCollection(userInfo.getOpenid(), parentInfo.getId());
		model.addAttribute("list", list);
		ImgGroup group = new ImgGroup();
		group.setPosition("1");
		group.setShelves(1);
		model.addAttribute("topBannerList",contentManageService.selectListByParam(group));
		return "user/collection";
	}
	
	/**
	 * 用户积分查询兑换
	 * @auther 胡启萌
	 * @Date 2016年11月21日
	 * @param model
	 * @return
	 */
	@RequestMapping("points")
	public String points(Model model) {
		WechatUserInfo userInfo = getWechatUserInfo();
		ParentInfo parent = parentInfoService.selectByOpenid(userInfo.getOpenid());
		model.addAttribute("parent", parent);
		Page<CourseSignOrderView> page = new Page<>();
		page.setPageSize(Integer.MAX_VALUE - 1);
		Map<String, Object> map = new HashMap<>();
		map.put("orderStatus", "000");
		page.setParams(map);
		List<CourseSignOrderView> list = productOrderService.selectCourseSignOrderByPage(page);
		model.addAttribute("list", list);
		return "user/points";
	}
	
	@RequestMapping("account")
	public String userAccount(Model model) {
		WechatUserInfo userInfo = getWechatUserInfo();
		ParentInfo parent = parentInfoService.selectByOpenid(userInfo.getOpenid());
		model.addAttribute("parent", parent);
		return "user/account";
	}
}
